import {resolve} from 'path'
import {loadEnv} from 'vite'
import type {UserConfig, ConfigEnv} from 'vite'
import vue from '@vitejs/plugin-vue'
import UnoCSS from 'unocss/vite'
import progress from 'vite-plugin-progress'
import PurgeIcons from 'vite-plugin-purge-icons'
import {createSvgIconsPlugin} from 'vite-plugin-svg-icons'
import {createStyleImportPlugin, ElementPlusResolve} from 'vite-plugin-style-import'
import VueI18nPlugin from "@intlify/unplugin-vue-i18n/vite"
import { ViteEjsPlugin } from "vite-plugin-ejs"
import { viteMockServe } from 'vite-plugin-mock'
import { visualizer } from 'rollup-plugin-visualizer'
// 获取项目根目录
const root = process.cwd()

function pathResolve(dir: string) {
    return resolve(root, '.', dir)
}

export default ({command, mode}: ConfigEnv): UserConfig => {
    let env = {} as any
    const isBuild = command === 'build'
    if (!isBuild) {
        env = loadEnv((process.argv[3] === '--mode' ? process.argv[4] : process.argv[3]), root)
    } else {
        env = loadEnv(mode, root)
    }
    return {
        base: env.VITE_BASE_PATH,
        plugins: [
            vue(),
            progress(),
            env.VITE_USE_ALL_ELEMENT_PLUS_STYLE === 'false'
                ? createStyleImportPlugin({
                    resolves: [ElementPlusResolve()],
                    libs: [
                        {
                            libraryName: 'element-plus',
                            esModule: true,
                            resolveStyle: (name) => {
                                if (name === 'click-outside') {
                                    return ''
                                }
                                return `element-plus/es/components/${name.replace(/^el-/, '')}/style/css`
                            }
                        }
                    ]
                })
                : undefined,
            createSvgIconsPlugin({
                iconDirs: [pathResolve('src/assets/svgs')],
                symbolId: 'icon-[dir]-[name]',
                svgoOptions: true
            }),
            env.VITE_USE_MOCK === 'true'
                ? viteMockServe({
                    ignore: /^\_/,
                    mockPath: 'mock',
                    localEnabled: !isBuild,
                    prodEnabled: isBuild,
                    injectCode: `
          import { setupProdMockServer } from '../mock/_createProductionServer'

          setupProdMockServer()
          `
                })
                : undefined,
            PurgeIcons(),
            VueI18nPlugin({
                runtimeOnly: true,
                compositionOnly: true,
                include: [resolve(__dirname, 'src/locales/**')]
            }),
            ViteEjsPlugin({
                title: env.VITE_APP_TITLE
            }),
            UnoCSS()

        ],
        css: {
            preprocessorOptions: {
                less: {
                    additionalData: '@import "./src/styles/variables.module.less";',
                    javascriptEnabled: true
                }
            }
        },
        resolve: {
            extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue'],
            alias: [
                {find: '@', replacement: resolve(__dirname, './src')},
            ]
        },
    }
}

